package com.tl.flowable.controller;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/flow/process")
public class FlowableController {

    //
    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    private final String YuanGong = "LiSi";
    private final String BanZhang = "ZhaoSi";
    private final String JingLi = "WangYiBo";

    @GetMapping(value = "/start")
    public String startProcess(){
        Map<String,Object> map = new HashMap<>();
        map.put("yuangong",YuanGong);
        ProcessInstance test = runtimeService.startProcessInstanceByKey("test",map);
        runtimeService.setVariable(test.getId(),"name","LiSi");
        runtimeService.setVariable(test.getId(),"content","身体不适");
        runtimeService.setVariable(test.getId(),"time","10Days");
        return test.getId();
    }

    @GetMapping(value = "/queryPi")
    public void queryProcess(){
        List<ProcessInstance> test = runtimeService.createProcessInstanceQuery().processDefinitionKey("test").orderByProcessDefinitionId().desc().list();
        for (ProcessInstance pi : test) {
            System.out.println("示例ID：" + pi.getId());
            Object name = runtimeService.getVariable(pi.getId(), "name");
            System.out.println(name);
            Object content = runtimeService.getVariable(pi.getId(), "content");
            System.out.println(content);
        }
    }

    @GetMapping(value = "/next")
    public String nextProcess(){
        Map<String,Object> map = new HashMap<>();
        map.put("banzhang",BanZhang);
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(this.YuanGong).orderByTaskId().desc().list();
        for (Task task : tasks) {
            System.out.println("任务编号：" + task.getId());
            System.out.println("任务处理人：" + task.getAssignee());
            System.out.println("任务是否挂起：" + task.isSuspended());
            taskService.complete(task.getId(),map);
        }
        return "提交下一节点";
    }

    @GetMapping(value = "/approve")
    public void approveTask(){
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(this.BanZhang).orderByTaskId().desc().list();
        for (Task task : tasks) {
            System.out.println("任务编号：" + task.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("jingli",this.JingLi);
            map.put("checkResult","通过");
            taskService.complete(task.getId(),map);
        }
    }

    @GetMapping(value = "/reject")
    public void rejectTask(){
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(this.JingLi).orderByTaskId().desc().list();
        for (Task task : tasks) {
            System.out.println("任务编号：" + task.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("checkResult","拒绝");
            taskService.complete(task.getId(),map);
        }
    }

}
